<?php

declare(strict_types=1);

namespace app\shop\controller\admin\goods;

use think\Request;
use think\facade\Db;
use sheep\controller\Backend;
use sheep\exception\SheepException;
use app\shop\model\goods\SkuPrice as SkuPriceModel;
use app\shop\model\goods\StockWarning as StockWarningModel;
use app\shop\traits\StockWarning as StockWarningTrait;

/**
 * 库存预警
 */
class StockWarning extends Backend
{
    use StockWarningTrait;
    
    public function initialize()
    {
        $this->model = new StockWarningModel;
    }


    /**
     * 库存预警列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $skuPriceTableName = (new SkuPriceModel())->db()->getTable();
        $stockWarnings = $this->model->sheepFilter()->alias('g')->with(['goods' => function ($query) {
                $query->removeOption('soft_delete');
            }])
            ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
            ->field('g.*,sp.stock')
            ->paginate(request()->param('list_rows', 10));

        $warning_total = $this->model->sheepFilter(false, function ($filters) {
                $filters['stock_type'] = 'no_enough';
                return $filters;
            })->alias('g')
            ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
            ->field('g.*,sp.stock')
            ->count();
        $over_total = $this->model->sheepFilter(false, function ($filters) {
                $filters['stock_type'] = 'over';
                return $filters;
            })->alias('g')
            ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
            ->field('g.*,sp.stock')
            ->count();

        $result = [
            'rows' => $stockWarnings,
            'warning_total' => $warning_total,
            'over_total' => $over_total,
        ];

        return success('获取成功', $result);
    }




    /**
     * 补货
     *
     * @param [type] $ids
     * @param [type] $stock
     * @return void
     */
    public function addStock ($id) {
        $params = $this->request->only(['id', 'stock']);
        $this->svalidate($params, ".add");

        $stockWarning = $this->model->with(['sku_price'])->where('id', $params['id'])->findOrFail();
        if (!$stockWarning->sku_price) {
            return error('库存规格不存在');
        }

        Db::transaction(function () use ($stockWarning, $params) {
            // 补货
            $this->addStockToSkuPrice($stockWarning->sku_price, $params['stock'], 'stock_warning');
        });

        return success('补货成功');
    }


    public function recyclebin()
    {
        $skuPriceTableName = (new SkuPriceModel())->db()->getTable();
        $stockWarnings = $this->model->onlyTrashed()->sheepFilter()->alias('g')->with(['goods' => function ($query) {
                $query->removeOption('soft_delete');
            }])
            ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
            ->field('g.*,sp.stock')
            ->paginate(request()->param('list_rows', 10));

        return success('获取成功', $stockWarnings);
    }
}
